package com.itheima.reggie.controlller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.DTO.SetmealDto;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.service.impl.CategoryService;
import com.itheima.reggie.service.impl.SetmealDishService;
import com.itheima.reggie.service.impl.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        setmealService.updateWithDish(setmealDto);
        return R.success("更新套餐成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        /*套餐分类 给的是分类的 id  前端想要中文名称  用SermealDTO*/

        //分页构造器
        Page<Setmeal> pageInfo=new Page<>(page,pageSize);
        //为了对象拷贝 因为SetmealDto里面有 CategroyName
        Page<SetmealDto> dtoPage=new Page<>();

        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        //添加查询条件 ，根据那么进行模糊查新
        queryWrapper.like(name!=null,Setmeal::getName,name);
        //根据更新条件 降序排列
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> list=records.stream().map((item)->{
            SetmealDto setmealDto=new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);

            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if(category!=null){
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }

    /**
     * 删除套餐
     * @return
     */
    @DeleteMapping
    public  R<String> delete(@RequestParam List<Long> ids){
        //删除 套餐时  套餐菜品表里面的数据也要删除
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    /**
     * 启售
     */
    @PostMapping("/status/{status}")
    public R<String> qishou(@PathVariable int status,Long ids){
        Setmeal setmeal=new Setmeal();
        setmeal.setId(ids);
        setmeal.setStatus(status);
        setmealService.updateById(setmeal);
        return R.success("售卖状态修改成功");
    }

    /**
     * 修改套餐时回显示
     */
    @GetMapping("/{id}")
    public R<SetmealDto> backshow(@PathVariable Long id){
        //id === setmeal里面的id 拿着这个套餐id 去 SetmeamDish里面去找 返回一个集合(SetmealDish)
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> SetmealDishlist = setmealDishService.list(queryWrapper);
        //获取setmeal对象  把setmeal属性 赋值给 SetmealDto中
        Setmeal setmeal = setmealService.getById(id);
        SetmealDto setmealDto=new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        //最后再Setmeal中setSetmealDishes

        setmealDto.setSetmealDishes(SetmealDishlist);
        return R.success(setmealDto);
    }

        @GetMapping("/list")
        public R<List<Setmeal>> list(Setmeal setmeal){
            LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(setmeal.getCategoryId() !=null,Setmeal::getCategoryId,setmeal.getCategoryId());
            queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
            List<Setmeal> setmeals = setmealService.list(queryWrapper);

            return R.success(setmeals);
        }
}
